package com.example.shop.service;

import com.example.shop.dao.ProductsDao;
import com.example.shop.entity.Products;
import com.example.shop.model.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

/**
 * author: adnaan
 * create: 2023-11-07 14:59
 * Description:
 */
@Service
public class ProductsService {

    @Autowired
    private ProductsDao productsDao;

    @Autowired
    private MongoTemplate mongoTemplate;

    public Products findDetail(String id) {
        return productsDao.findDetail(id);
    }

    public ApiResponse page(Integer currentPage, Integer pageSize, String productName) {
        return productsDao.page(currentPage,pageSize,productName);
    }

    public ApiResponse addProduct(Products products) {
        return productsDao.save(products);
    }

    public ApiResponse saveProduct(Products products) {
        Query query=new Query(Criteria.where("id").is(products.getId()));
        Update update=new Update();
        update.set("name",products.getName());
        update.set("price",products.getPrice());
        update.set("description",products.getDescription());
        update.set("image",products.getImage());
        return productsDao.update(query,update);
    }

    public ApiResponse delProduct(String productId) {
        Criteria criteriaName = Criteria.where("id").is(productId);
        Criteria orCriteria = new Criteria();
        orCriteria.orOperator(criteriaName);
        Query query = new Query(orCriteria);
        return productsDao.delete(query);
    }
}
